﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Transactions;

namespace RockBus.DataContracts
{
    [CollectionDataContract(Namespace = StringConstants.RockBusNamespace)]
    public class EndpointInfoCollection : Collection<EndpointInfo>
    {
        public EndpointInfo this[string id]
        {
            get
            {
                return this.FirstOrDefault<EndpointInfo>((ei) => StringComparer.InvariantCultureIgnoreCase.Compare(ei.Id, id) == 0);
            }
        }

        public void AddRange(IEnumerable<EndpointInfo> endpoints)
        {
            foreach (var endpoint in endpoints)
            {
                this.Add(endpoint);
            }
        }

        public bool ContainsRequestResponseEndpoint()
        {
            foreach (EndpointInfo ei in this)
            {
                if (ei.ExchangeType == ExchangeTypeEnum.TwoWayRequestResponse)
                {
                    return true;
                }
            }

            return false;
        }

        public bool ContainsTransactionalEndpoint()
        {
            foreach (EndpointInfo ei in this)
            {
                if (ei.TransactionScopeOption != TransactionScopeOption.Suppress)
                {
                    return true;
                }
            }

            return false;
        }
    }
}